import router from "./router";
import store from './store'
import {
    getToken
} from "@/utils/token";
router.beforeEach(async (to, from, next) => {
    //如果是登录页
    if (to.path == "/login") {
        next(); //不处理任何拦截
    } else {
        if (getToken()) {
            //判断有没有角色
            //如果有角色
            var getRoles = store && store.getters.roles && store.getters.roles.length > 0
            if (getRoles) {
                next();
            } else {
                //获取角色信息,发送请求
                let {
                    roles
                } = await store.dispatch('getInfo')
                console.log(roles, 'roles');
                //取出角色
                var rolesName = roles.map(v => v.name)
                //过滤角色
                var filterRouters = await store.dispatch('GENERATEROUTES', rolesName)
                //动态添加
                router.addRoutes(filterRouters)
                if (roles) {
                    next({
                        path: to.path
                    });
                } else {
                    next({
                        path: '/login'
                    });
                }
            }
        } else {
            next({
                path: '/login'
            });
        }
    }
})